package cn.jiyun.function.service.impl;

import cn.jiyun.function.mapper.CarMapper;
import cn.jiyun.function.mapper.DriverMapper;
import cn.jiyun.function.service.CarService;
import cn.jiyun.pojo.function.Car;
import cn.jiyun.pojo.function.Driver;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@Slf4j
public class CarServiceImpl implements CarService {
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private DriverMapper driverMapper;

    @Override
    public PageInfo<Car> findPage(Integer pageNum, Integer pageSize, Car car1) {
        Page<Car> page = PageHelper.startPage(pageNum, pageSize);
        List<Car> list = carMapper.findPage(page, car1);
        log.info("list:{}", list);
        return new PageInfo<>(list);
    }

    @Override
    public Car findCarById(Integer id) {
        Car carById = carMapper.findCarById(id);
        List<Driver> driverList = driverMapper.findList();
        driverList.forEach(driver -> {
            if (carById.getDid().equals(driver.getUserId())) {
                carById.setUserName(driver.getUserName());
                carById.setUserPhoto(driver.getUserPhoto());
                carById.setUserPhone(driver.getUserPhone());
                carById.setUserType(driver.getUserType());
                carById.setUserSex(driver.getUserSex());
                carById.setUserBirthday(driver.getUserBirthday());
                carById.setUserAge(driver.getUserAge());
                carById.setUserEmail(driver.getUserEmail());
                carById.setDrivingLicense(driver.getDrivingLicense());
                carById.setRegisteredCity(driver.getRegisteredCity());
                carById.setAdministrativeRegion(driver.getAdministrativeRegion());
                carById.setDregistrationTime(driver.getRegistrationTime());
            }
        });
        return carById;
    }

    @Override
    public void updateVerificationStatus(Integer id, Integer status) {

        String verifier = "管理员";
        LocalDateTime approvalTime = LocalDateTime.now();
        carMapper.updateVerificationStatus(id, status, verifier, approvalTime);
    }
}
